﻿using NewsCollection.Data;
using NewsCollection.Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NewsCollection.Common.Service
{
    class ProcessDb
    {
        DataContext db = new DataContext("name=news");
        SqlConnection cn = new SqlConnection
         (@"Data Source=sonic;Initial Catalog=dbNewsCollection;Integrated Security=True");


        SqlCommand cmd = new SqlCommand();
        SqlDataReader dr;
        public Category CheckCategoryDb(string sId)
        {
            var list = db.Categories.ToList();
            if (list.Count() > 0)
                for (int i = 0; i < list.Count(); i++)
                {
                    if (list[i].sId != null)
                        if (list[i].sId.Equals(sId))
                        {
                            return list[i];
                          
                        }

                }
            return null;
        }
     
    
        public Manufactory CheckMenufactoryDb(string Url)
        {
            var list = db.Manufactory.ToList();
            if (list.Count() > 0)
                for (int i = 0; i < list.Count(); i++)
                {
                    if (list[i].Url != null)
                        if (list[i].Url.Equals(Url))
                        {
                            return list[i];

                        }

                }
            return null;
        }
        public Manu_Cate_Mapping CheckManuCateMapping(int manuId,int cateId)
        {

            var list = db.Manu_Cate_Mapping.Where(m=>m.Manufactory.Id==manuId).ToList();
            if (list.Count() > 0)
                for (int i = 0; i < list.Count(); i++)
                {
                    if (list[i].Category.Id == cateId)
                       
                            return list[i];

                        

                }
            return null;
        }
     

     
       
        public News SearchNews(String urlNews)
        {
             var item = db.News.FirstOrDefault(m=>m.Url.Equals(urlNews));
        
            
                    

                        return item;



                
            
        }
        public CategoryManuals CheckCategoryManual(int Id)
        {
            var list = db.CategoryManual.ToList();
            if (list.Count() > 0)
                for (int i = 0; i < list.Count(); i++)
                {
                    if (list[i].Id == Id)

                        return list[i];



                }
            return null;
        }
        public Category SearchCategory(int id)
        {
            var list =  db.Categories.ToList();
            for (int i = 0; i < list.Count(); i++)
            {
                if (list[i].Id == id)
                {
                    return list[i];
                    
                }
            }
            return null;

        }
        public Manufactory SearchManufactory(int id)
        {
            var list = db.Manufactory.ToList();
            for (int i = 0; i < list.Count(); i++)
            {
                if (list[i].Id == id)
                {
                    return list[i];

                }
            }
            return null;
        }
        public CategoryManuals SearchCategoryManuals(string name)
        {
            var list = db.CategoryManual.ToList();
            for (int i = 0; i < list.Count(); i++)
            {
                if (list[i].MyName.Equals(name))
                {
                    return list[i];

                }
            }
            return null;
        }
        
    
        public News SaveNews(News item)
        {
           
                db.News.Add(item);
                db.SaveChanges();
                return item;
         
        }
      
     

      
        public News UpdateNews(News item)
        {
      
            db.SaveChanges();
            return item;
        }
  
      
     
        public Category SaveCategory(Category item)
        {
            db.Categories.Add(item);
            db.SaveChanges();
            return item;
        }
     
        public Manufactory SaveManufactory(Manufactory item)
        {
            if (CheckMenufactoryDb(item.Url) == null)
            {
                db.Manufactory.Add(item);
                db.SaveChanges();
            }
            return item;
        }
       
        public Manu_Cate_Mapping SaveManuCateMapping(Manu_Cate_Mapping item)
        {
            db.Manu_Cate_Mapping.Add(item);
            db.SaveChanges();
            return item;
        }
        public CategoryManuals SaveCategoryManual(CategoryManuals item)
        {
            if (SearchCategoryManuals(item.MyName) == null)
            {
                db.CategoryManual.Add(item);
                db.SaveChanges();
                return item;
            }
            return null;
        }
        // luu hinh` featured

        // luu hinh` news
        public void SaveImgFeatured(string folder, NewsItemModel item)
        {
            Image imgFirst = Extensions.ConvertByteToImage(item.FirstImage);
            Image img = imgFirst;
            
            img.SaveImageToFolder(folder + "/Normal/Featured" + "/" + item.Id + " _FirstImage.jpg");
            img.Crop(200, 130, Extensions.AnchorPosition.Top).SaveImageToFolder(folder + "/200x130/Featured" + "/" + item.Id + " _FirstImage.jpg");
            img.Crop(315, 240, Extensions.AnchorPosition.Top).SaveImageToFolder(folder + "/315x240/Featured" + "/" + item.Id + " _FirstImage.jpg");

            if (item.SecondImage == null)
            {
                Image img1 = imgFirst;
                img1.ResizeImageFixedWidth(700).Crop(700, 256, Extensions.AnchorPosition.Top).SaveImageToFolder(folder + "/700x256/Featured" + "/" + item.Id + " _FirstImage.jpg");


            }
            else
            {
                Image imgTemp = imgFirst;
                Image img1 = imgTemp.ResizeImageFixedWidth(350);
                Image img2 = Extensions.ConvertByteToImage(item.SecondImage).ResizeImageFixedWidth(350);
                Image img3 = Extensions.MergeTwoImages(img1, img2);
                img3.ResizeImageFixedWidth(700).Crop(700, 256, Extensions.AnchorPosition.Top).SaveImageToFolder(folder + "/700x256/Featured" + "/" + item.Id + " _FirstImage.jpg");

            }
        
                                
        }
        public void SaveImgNormal(string folder, NewsItemModel item)
        {
            Image imgFirst = Extensions.ConvertByteToImage(item.FirstImage);
            Image img = imgFirst;
            
            img.SaveImageToFolder(folder + "/Normal/News" + "/" + item.Id + " _FirstImage.jpg");
            img.Crop(200, 130, Extensions.AnchorPosition.Top).SaveImageToFolder(folder + "/200x130/News" + "/" + item.Id + " _FirstImage.jpg");
            img.Crop(315, 240, Extensions.AnchorPosition.Top).SaveImageToFolder(folder + "/315x240/News" + "/" + item.Id + " _FirstImage.jpg");
            if (item.SecondImage == null)
            {
                Image img1 = imgFirst;
                img1.ResizeImageFixedWidth(700).Crop(700, 256, Extensions.AnchorPosition.Top).SaveImageToFolder(folder + "/700x256/News" + "/" + item.Id + " _FirstImage.jpg");


            }
            else
            {
                Image imgTemp = imgFirst;
                Image img1 = imgTemp.ResizeImageFixedWidth(350);
                Image img2 = Extensions.ConvertByteToImage(item.SecondImage).ResizeImageFixedWidth(350);
                Image img3 = Extensions.MergeTwoImages(img1, img2);
                img3.ResizeImageFixedWidth(700).Crop(700, 256, Extensions.AnchorPosition.Top).SaveImageToFolder(folder + "/700x256/News" + "/" + item.Id + " _FirstImage.jpg");

            }

        }
     

        public  void RecursiveCategoryManual(TreeNode node)
        {
         
            for (int i = 0; i < node.Nodes.Count; i++)
            {
                var item = node.Nodes[i];
                var parentNode = item.Parent;
                var parent = SearchCategoryManuals(parentNode.Text);
                var listSubCateManual = db.CategoryManual.Where(m => m.ParentCategoryManual.MyId.Equals(parent.MyId) && m.ParentCategoryManual.MyName.Equals(parent.MyName)).ToList();
                var listSubCate = db.Categories.Where(m => m.CategoryManual.MyId.Equals(parent.MyId) && m.CategoryManual.MyName.Equals(parent.MyName)).ToList();
               //neu so luong sub trong db nho hon trong tree thj` xu ly
                if (node.Nodes.Count > listSubCate.Count()+listSubCateManual.Count())
                    for (int j = 0; j < node.Nodes.Count; j++)
                {
                    int id = SearchCategory(Convert.ToInt32(node.Nodes[j].Name)).Id;
                    cn.Open();
                    cmd.CommandText = "Update Categories set CategoryManual_Id='" + parent.Id + "' where Id='" + id + "'Update Categories set CategoryManual_Id='" + parent.Id + "' where ParentCategory_Id='" + id + "'";
                 //   cmd.CommandText = "Update Categories set CategoryManual_Id='" + parent.Id + "' where ParentCategory_Id='" + id + "'";

                    //  cmd.CommandText = "select * from Categories";
                    // SqlCommand comand = new SqlCommand(cmd, cn);
                    cmd.Connection = cn;
                    cmd.ExecuteNonQuery();
                    cn.Close();
                }
                else
                  
                {
                    var isCateManual = SearchCategoryManuals(item.Text);
                    if (isCateManual != null) 
                     RecursiveCategoryManual(item); 
                }
            }
        }
    }
}
